package org.example.frw.verify.annotations;

import cn.hutool.core.lang.Assert;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.example.frw.verify.DoVerify;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class VerifyAspect {  
  
    @Around("@annotation(verify)")
    public Object verifyAdvice(ProceedingJoinPoint joinPoint, org.example.frw.verify.annotations.Verify verify) throws Throwable {
        // 在方法执行前执行逻辑  
        log.info("Verifying method: {}", joinPoint.getSignature().getName());
        DoVerify bean = SpringUtil.getBean(verify.verifalClass());
        Assert.notNull(bean,"无法找到[{}]",verify.verifalClass());
        // 检查参数合法性
        bean.verify(joinPoint.getArgs());
        // 执行原方法
        try {
            return joinPoint.proceed();
        }finally {
            bean.clear();
        }
    }
}